/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package idv.cloudee.proxy2.impl.getpost;

import com.cirnoworks.http.utils.HeaderRequest;
import idv.cloudee.proxy2.impl.*;
import com.cirnoworks.http.utils.exception.BadRequestException;
import idv.cloudee.proxy2.framework.ProxyThread;
import idv.cloudee.proxy2.intf.RequestDealer;
import idv.cloudee.proxy2.intf.RequestDealerFactory;
import java.util.ArrayList;

/**
 *
 * @author Cloudee
 */
public class GETPOSTDealerFactory implements RequestDealerFactory {

//    private final WeakHashMap<ProxyThread, GetPostOverHttpDealer> threads = new WeakHashMap<ProxyThread, GetPostOverHttpDealer>();
    private final ArrayList<GETPOSTModifierFactory> gpmfs = new ArrayList<GETPOSTModifierFactory>();
    private String httpUrl;
    private String proxyAddress;
    private int proxyPort;

    public String getHttpUrl() {
        return httpUrl;
    }

    public void setHttpUrl(String httpUrl) {
        this.httpUrl = httpUrl;
    }

    public String getProxyAddress() {
        return proxyAddress;
    }

    public void setProxyAddress(String proxyAddress) {
        this.proxyAddress = proxyAddress;
    }

    public int getProxyPort() {
        return proxyPort;
    }

    public void setProxyPort(int proxyPort) {
        this.proxyPort = proxyPort;
    }

    public void addGETPOSTModifierFactory(GETPOSTModifierFactory gpmf) {
        synchronized (gpmfs) {
            gpmfs.add(gpmf);
        }
    }

    public void clearGETPOSTModifierFactories() {
        synchronized (gpmfs) {
            gpmfs.clear();
        }
    }

    public RequestDealer getRequestDealer(HeaderRequest reqHeaderParam, ProxyThread thread) throws BadRequestException {
        GETPOSTDealer ret;
//        synchronized (threads) {
//            gpd = threads.get(thread);
//            if (gpd == null) {
        if (httpUrl != null && httpUrl.length() > 0) {
            GETPOSTDealerOverHttp gpd = new GETPOSTDealerOverHttp();
            gpd.setHttpUrl(httpUrl);
            gpd.setProxyAddress(proxyAddress);
            gpd.setProxyPort(proxyPort);
            for (GETPOSTModifierFactory gpmf : gpmfs) {
                GETPOSTModifier modifier = gpmf.getGETPOSTModifier();
                if (modifier != null) {
                    gpd.addModifier(modifier);
                }
            }
            gpd.addModifier(new DefaultGETPOSTModifier());
            ret = gpd;
        } else {
            if (proxyAddress != null && proxyAddress.length() > 0) {
                GETPOSTDealerOverProxy gpd = new GETPOSTDealerOverProxy();
                for (GETPOSTModifierFactory gpmf : gpmfs) {
                    GETPOSTModifier modifier = gpmf.getGETPOSTModifier();
                    if (modifier != null) {
                        gpd.addModifier(modifier);
                    }
                }
                gpd.setProxyAddress(proxyAddress);
                gpd.setProxyPort(proxyPort);
                ret = gpd;
            } else {
                GETPOSTDealerOverSocket gpd = new GETPOSTDealerOverSocket();
                for (GETPOSTModifierFactory gpmf : gpmfs) {
                    GETPOSTModifier modifier = gpmf.getGETPOSTModifier();
                    if (modifier != null) {
                        gpd.addModifier(modifier);
                    }
                }
                gpd.addModifier(new DefaultGETPOSTModifier());
                ret = gpd;
            }
        }

        return ret;
    }
}
